package com.key.win.system.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.key.win.system.model.SysDevice;
import com.key.win.system.model.SysUser;
import com.key.win.system.model.SysUserDevice;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;


/**
 * 用户机构关系
 */
@Mapper
public interface SysUserDeviceDao extends BaseMapper<SysUserDevice> {

    int deleteUserDevice(@Param("userId") String userId, @Param("deviceId") String deviceId, @Param("organId") String organId);

    /**
     * 根据用户id获取Device
     *
     * @param userId
     * @return
     */
    @Select("select o.*,uo.organ_id from sys_user_device uo inner join sys_device o on uo.device_id = o.id where uo.user_id = #{userId} and uo.enable_flag = 1 and o.enable_flag = 1")
    List<SysDevice> findDeviceByUserId(String userId);

    /**
     * 根据用户id获取Device
     *
     * @param deviceId
     * @return
     */
    @Select("select u.* from sys_user_device uo inner join sys_user u on uo.user_id = u.id where uo.device_id = #{deviceId} and uo.enable_flag = 1 and u.enable_flag = 1")
    List<SysUser> findUserByDeviceId(String deviceId);

    /**
     * 根据用户ids 获取
     *
     * @param userIds
     * @return
     */

    @Select("<script>select r.*,uo.user_id from sys_user_device uo inner join sys_device r on r.id = ru.device_id where  ru.enable_Flag = 1 and uo.enable_Flag = 1 and ru.user_id IN " +
            " <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'> " +
            " #{item} " +
            " </foreach>" +
            "</script>")
    List<SysDevice> findDeviceByUserIds(List<String> userIds);

    /**
     * 根据用户ids 获取
     *
     * @param deviceIds
     * @return
     */

    @Select("<script>select u.*,uo.device_Id from sys_user_device uo inner join sys_user u on u.id = uo.user_id where u.enable_Flag = 1 and uo.enable_Flag = 1  and ru.device_id IN " +
            " <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'> " +
            " #{item} " +
            " </foreach>" +
            "</script>")
    List<SysUser> findUserByDeviceIds(List<String> deviceIds);

    void saveBatchUserIdAndDeviceIds(@Param("userId") String userId, @Param("deviceIds") Set<String> deviceIds);

    void saveBatchUserIdAndOrganIdsAndDeviceIds(@Param("userId") String userId, @Param("organIdsAndDeviceIds") List<SysUserDevice> organIdsAndDeviceIds);

    void saveBatchUserIdsAndDeviceId(@Param("userIds") Set<String> userIds, @Param("deviceId") String deviceId);

    void saveBatchUserIdsAndOrganIdsAndDeviceId(@Param("userIdsAndOrganIds") List<SysUserDevice> userIdsAndOrganIds, @Param("deviceId") String deviceId);
}
